﻿using System;
using System.Windows;
using System.Windows.Interactivity;
using System.Windows.Media.Animation;
using PairingMatrix.ViewModel;

namespace PairingMatrix.View.Behavior
{
    public class HighlightProjectCardBehavior : Behavior<ProjectCardControl>
    {
        private ProjectCardViewModel _projectCardViewModel;
        private Storyboard _storyboard;
        private DoubleAnimation _scaleAnimationX;
        private DoubleAnimation _scaleAnimationY;

        protected override void OnAttached()
        {
            AssociatedObject.Loaded += (sender, e) =>
            {
                _projectCardViewModel = (ProjectCardViewModel)AssociatedObject.DataContext;
                _projectCardViewModel.PropertyChanged += ProjectCardPropertyChanged;

                //animation for pairwith controls
                _scaleAnimationX = new DoubleAnimation();
                _scaleAnimationY = new DoubleAnimation();
                TimeSpan duration = TimeSpan.FromMilliseconds(100);
                _scaleAnimationX.Duration = duration;
                _scaleAnimationY.Duration = duration;
                _storyboard = new Storyboard();
                _storyboard.Duration = duration;
                _storyboard.Children.Add(_scaleAnimationX);
                _storyboard.Children.Add(_scaleAnimationY);
                Storyboard.SetTarget(_scaleAnimationX, AssociatedObject.scaleTransform);
                Storyboard.SetTarget(_scaleAnimationY, AssociatedObject.scaleTransform);
                Storyboard.SetTargetProperty(_scaleAnimationX, new PropertyPath("ScaleX"));
                Storyboard.SetTargetProperty(_scaleAnimationY, new PropertyPath("ScaleY"));
            };

        }

        void ProjectCardPropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
        {
            if (e.PropertyName == "ShowHighlighted")
            {
                if (_projectCardViewModel.ShowHighlighted)
                {
                    _scaleAnimationX.To = 1.4;
                    _scaleAnimationY.To = 1.4;
                    _storyboard.Begin();
                }
                else
                {
                    _scaleAnimationX.To = 1.0;
                    _scaleAnimationY.To = 1.0;
                    _storyboard.Begin();
                }
                
            }
        }

    }
}
